import {Web} from '../../config'

export default class FuncDataGet{
	static createDataByInputAsync(stepMeta,pageRect,translate,ratio){
		let fn = new Function("return "+stepMeta.fnExpr)()
		return new Promise((resolve,reject)=>{
			let method = stepMeta.eq?"createEqData":"createFnData"
			let ps = FuncDataGet[method](fn,stepMeta,pageRect,translate,ratio)
			resolve(ps)
		})
	}
	static createDataByInput(stepMeta,pageRect,translate,ratio){
		let fn = new Function("return "+stepMeta.fnExpr)()
		let method = stepMeta.eq?"createEqData":"createFnData"
		return FuncDataGet[method](fn,stepMeta,pageRect,translate,ratio)
	}
	static createEqData(fn,stepMeta,pageRect,translate,ratio){
		let ps = []
		let arg = stepMeta.arg
		let method = "createEqDataBy"+arg
		ps = FuncDataGet[method](fn,pageRect,translate,ratio)
		return ps
	}
	static createEqDataByxy(fn,pageRect,translate,ratio){
		let {width,height} = pageRect
		let {tx,ty} = translate
		let ps = []
		for(let i=-ty;i<height-ty;i++){
			let seed = i||Web.minimum
			let y = seed
			let solveXs = FuncDataGet.solveEq(fn,width,tx,ratio,y)
			let p = solveXs.map(x=>{
				return {x,y:-y}
			})
			if(!p.isEmpty()){
				//fix开头和结尾由于精度0.3px的求解问题不能闭合
				ps.isEmpty()&&ps.push([{x:p[0].x,y:-y+0.5}])
				ps.push(p)
			}
		}
		//fix开头和结尾由于精度0.3px的求解问题不能闭合
		if(!ps.isEmpty()){
			let lp = ps.last()[0]
			ps.push([{x:lp.x,y:lp.y-0.5}])
		}
		console.log("solovexy",ps.length)
		return [ps]
	}
	static createEqDataByx(fn,pageRect,translate,ratio){
		let {width,height} = pageRect
		let {tx,ty} = translate
		let res = FuncDataGet.solveEq(fn,width,tx,ratio)
		console.log("solvex",res.length)
		if(res.isEmpty())return []
		return res.map(solve=>{
			let ps = []
			for(let i=-ty;i<height-ty;i++){
				let seed = i||Web.minimum
				let y = seed
				let x = solve
				ps.push({x,y})
			}
			return ps
		})
	}
	static createEqDataByy(fn,pageRect,translate,ratio){
		let {width,height} = pageRect
		let {tx,ty} = translate
		let res = FuncDataGet.solveEq(fn,height,ty,ratio)
		console.log("solvey",res.length)
		if(res.isEmpty())return []
		return res.map(solve=>{
			let ps = []
			for(let i=-tx;i<width-tx;i++){
				let seed = i||Web.minimum
				let x = seed
				let y = -solve
				ps.push({x,y})
			}
			return ps
		})
	}
	static solveEq(fn,span,offset,ratio,eq=0){
		let res = []
		let c = 0
		let flag = false
		let flag1 = false
		for(let i=-offset;i<span-offset;i+=0.3){
			let seed = i||Web.minimum
			let rs = fn(seed/ratio,eq/ratio)*ratio
			if(c++==0){
				flag = rs<=0
				flag1 = rs<=0
			}else {
				flag = rs<=0
				if(flag!==flag1){
					res.push((seed+seed-1)/2)
					flag1=flag
				}
			}
		}
		return res
	}
	static createFnData(fn,stepMeta,pageRect,translate,ratio){
		let ps = []
		let arg = stepMeta.arg
		let method = stepMeta.arg==="x"?"createFnDataByX":"createFnDataByY"
		ps = FuncDataGet[method](fn,pageRect,translate,ratio)
		return [ps]
	}
	static createFnDataByX(fn,pageRect,translate,ratio){
		let ps = []
		let {width,height} = pageRect
		let {tx,ty} = translate
		// let mh = Math.max(Math.abs(ty),Math.abs(height-ty))
		for(let i=-tx;i<width-tx;i++){
			let seed = i||Web.minimum
			let x = seed
			let y = -fn(x/(ratio))*ratio
			// let absy = Math.min(Math.abs(y),mh)+1
			// y=absy*(y>0?1:-1)
			ps.push({x,y})
		}
		return ps
	}
	static createFnDataByY(fn,pageRect,translate,ratio){
		let ps = []
		let {width,height} = pageRect
		let {tx,ty} = translate
		// let mw = Math.max(Math.abs(tx),Math.abs(width-tx))
		for(let i=-ty;i<height-ty;i++){
			let seed = i||Web.minimum
			let y = seed
			let x = fn(-y/(ratio))*ratio
			// let absx = Math.min(Math.abs(x),mw)+1
			// x=absx*(x>0?1:-1)
			ps.push({x,y})
		}
		return ps
	}
}